package top.hooray.delayedpayment.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import top.hooray.delayedpayment.entity.Order;
import top.hooray.delayedpayment.service.OrderService;

import java.io.IOException;


/**
 * @author hooray
 * @date 2021/3/9 23:38
 */
@Component
public class OrderListener {

    private final OrderService orderService;

    public OrderListener(OrderService orderService) {
        this.orderService = orderService;
    }

    @RabbitListener(queues = "queue.order", ackMode = "MANUAL")
    public void onMessage(Order order, Channel channel, Message message) throws IOException {
        orderService.saveOrder(order);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
